package com.Customer.controller;

import com.Customer.entity.Customer;
import com.Customer.service.CustomerService;
import com.Customer.utils.ResponseResult;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * 客户信息 前后端交互类
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/api/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 	添加客户信息
     * @param customer
     * @throws IOException
     */
    @RequestMapping(value = "/add.do", produces = "application/json; charset=utf-8")
    @CircuitBreaker(name = "customerService", fallbackMethod = "fallbackAddCustomerInfo")
    public ResponseResult addCustomerInfo(@RequestBody Customer customer) throws IOException {
        // 添加客户信息
        if(customerService.doAddCustomerInfo(customer)) {
            // 添加客户信息成功
            return ResponseResult.build(true, "添加客户信息成功！");
        }else {
            // 添加客户信息失败
            return ResponseResult.build(false, "添加客户信息失败，可能是当前客户信息已存在！");
        }
    }

    /**
     * 添加客户信息失败时的降级方法
     */
    public ResponseResult fallbackAddCustomerInfo(@RequestBody Customer customer, Throwable throwable) {
        System.err.println("断路器触发 - 添加客户信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 	修改客户信息
     * @param customer
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/modify.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "customerService", fallbackMethod = "fallbackModifyCustomerInfo")
    public ResponseResult modifyCustomerInfo(@RequestBody Customer customer, HttpServletRequest request) throws IOException {
        // 修改客户信息
        if(customerService.doModifyCustomerInfo(customer)) {
            // 修改客户信息成功
            return ResponseResult.build(true, "修改客户信息成功！");
        }else {
            // 修改客户信息失败
            return ResponseResult.build(false, "修改客户信息失败，可能是当前客户信息不存在！");
        }
    }

    /**
     * 修改客户信息失败时的降级方法
     */
    public ResponseResult fallbackModifyCustomerInfo(@RequestBody Customer customer, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 修改客户信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 	删除客户信息
     * @param customer
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/delete.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "customerService", fallbackMethod = "fallbackDeleteCustomerInfo")
    public ResponseResult deleteCustomerInfo(Customer customer, HttpServletRequest request) throws IOException {
        // 删除客户信息
        if (customerService.doDeleteCustomerInfo(customer)) {
            // 删除客户信息成功
            return ResponseResult.build(true, "删除客户信息成功！");
        } else {
            // 删除客户信息失败
            return ResponseResult.build(false, "删除客户信息失败，可能是当前客户信息不存在！");
        }
    }

    /**
     * 删除客户信息失败时的降级方法
     */
    public ResponseResult fallbackDeleteCustomerInfo(Customer customer, HttpServletRequest request, Throwable throwable) {
        System.err.println("断路器触发 - 删除客户信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 	获取客户信息
     * @param customer
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/get.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "customerService", fallbackMethod = "fallbackGetCustomerInfo")
    public ResponseResult getCustomerInfo(Customer customer, HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取客户信息
        Customer pCustomer = customerService.doGetCustomerInfo(customer);
        // 返回数据
        return ResponseResult.build(pCustomer!=null, null, pCustomer);
    }

    /**
     * 获取客户信息失败时的降级方法
     */
    public ResponseResult fallbackGetCustomerInfo(Customer customer, HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 获取客户信息失败: " + throwable.getMessage());
        Customer defaultCustomer = new Customer();
        defaultCustomer.setCid(-1); // 假设设置一个默认的无效 ID
        return ResponseResult.build(false, "系统繁忙，返回临时数据", defaultCustomer);
    }

    /**
     * 	获取客户列表
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "/list.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "customerService", fallbackMethod = "fallbackGetCustomerInfoList")
    public ResponseResult getCustomerInfoList(HttpServletRequest request,HttpServletResponse response) throws IOException {
        // 获取客户列表
        List<Customer> list = customerService.doGetCustomerInfoList();
        // 返回数据
        return ResponseResult.build(list!=null, null, list);//ResponseResult是一个用于封装响应结果和数据的通用类
    }

    /**
     * 获取客户列表失败时的降级方法
     */
    public ResponseResult fallbackGetCustomerInfoList(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 获取客户列表失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，返回部分数据", Collections.emptyList());
    }

    /**
     * 	查询客户列表
     * @param customer
     * @param request
     * @throws IOException
     */
    @RequestMapping(value = "/query.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "customerService", fallbackMethod = "fallbackQueryCustomerInfoList")
    public ResponseResult queryCustomerInfoList(Customer customer,HttpServletRequest request,HttpServletResponse response) throws IOException {
        // 查询客户列表
        List<Customer> list = customerService.doQueryCustomerInfoList(customer);
        // 返回数据
        return ResponseResult.build(list!=null, null, list);
    }

    /**
     * 查询客户列表失败时的降级方法
     */
    public ResponseResult fallbackQueryCustomerInfoList(Customer customer, HttpServletRequest request, HttpServletResponse response, Throwable throwable) {
        System.err.println("断路器触发 - 查询客户列表失败: " + throwable.getMessage());
        return ResponseResult.success("查询部分数据", Collections.emptyList());
    }
}